package com.lzx.iteam.widgetview;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import android.widget.Toast;
import com.lzx.iteam.R;
import com.lzx.iteam.ScheduleCalendarActivity;
import com.lzx.iteam.bean.EventListData;
import com.lzx.iteam.bean.ScheduleData;
import com.lzx.iteam.bean.ScheduleListData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetCalendarMsg;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.provider.ScheduleDB;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.PreferenceUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 04月 06日
 * Time: 上午10:40
 *
 * @QQ : 1234567890
 * **********************************
 */
public class WidgetSetService extends RemoteViewsService {

    private ScheduleDB mScheduleDB;
    private String mDate;
    private String mUid;
    private List<Object> mCalendarData = new ArrayList<Object>();

    public WidgetSetService() {

    }
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new WidgetFactory(this.getApplicationContext(), intent);
    }

    public class WidgetFactory implements RemoteViewsFactory {
        private Context mContext;
        public WidgetFactory(Context context, Intent intent) {
            mContext = context;
        }

        @Override
        public void onCreate() {

            mScheduleDB = new ScheduleDB(mContext);
            Calendar calendar = Calendar.getInstance();
            int curYear = calendar.get(Calendar.YEAR);
            int curMonth = calendar.get(Calendar.MONTH) + 1; //通过Calendar算出的月数要+1
            int curDate = calendar.get(Calendar.DATE);
            mDate = curYear + "_" + curMonth + "_" + curDate;
            mUid = PreferenceUtil.getInstance(mContext).getString(PreferenceUtil.CLIENT_USEID, "－1");
            Log.d("onReceive_widget", mDate + "  onCreate  " + mUid);
            mCalendarData = mScheduleDB.findScheduleByDate(mDate,mUid);
            if (mCalendarData.size() == 0){
                mCalendarData.add("今天还没有日程");
            }

        }

        @Override
        public void onDataSetChanged() {
            /*
             * appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);
             * 使用该通知更新数据源，会调用onDataSetChanged
             */
            Log.d("onReceive_widget", "----onDataSetChanged----");
            mUid = PreferenceUtil.getInstance(mContext).getString(PreferenceUtil.CLIENT_USEID, "－1");
            mCalendarData = mScheduleDB.findScheduleByDate(mDate,mUid);
            if (mCalendarData.size() == 0){
                mCalendarData.add("今天还没有日程");
            }
        }

        @Override
        public void onDestroy() {
            mCalendarData.clear();
        }

        @Override
        public int getCount() {
            return  mCalendarData.size();
        }

        @Override
        public RemoteViews getViewAt(int position) {
            if (position < 0 || position >= getCount()) {
                return null;
            }
            RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item);
            if (mCalendarData.get(position) instanceof ScheduleData){
                ScheduleData data = (ScheduleData) mCalendarData.get(position);
                views.setTextViewText(R.id.widget_list_item_tv, data.getTitle());
                long startTime = Long.parseLong(data.getStartTime()) * 1000;
                views.setTextViewText(R.id.widget_list_item_time, DateUtil.getHourAndMin(startTime));

            }else if(mCalendarData.get(position) instanceof EventListData){
                EventListData data = (EventListData) mCalendarData.get(position);
                views.setTextViewText(R.id.widget_list_item_tv, data.getEventTitle());
                long startTime = Long.parseLong(data.getActivityTime()) * 1000;
                views.setTextViewText(R.id.widget_list_item_time, DateUtil.getHourAndMin(startTime));
            }else {
                views.setTextViewText(R.id.widget_list_item_time, "今天还没有日程");
            }

            /*// TODO 跳转到 WidgetSetProvider
            Intent fillInIntent = new Intent();
            fillInIntent.putExtra("position",position);
            views.setOnClickFillInIntent(R.id.widget_list_item_tv,fillInIntent);*/

            // TODO 进入日程页面
            Intent activity = new Intent(mContext,ScheduleCalendarActivity.class);
            //创建一个pendingIntent。另外两个参数以后再讲。
            PendingIntent pendingAc = PendingIntent.getActivity(
                    mContext, 0, activity, 0);
            //绑定处理器，表示控件单击后，会启动pendingIntent。
            views.setOnClickPendingIntent(R.id.widget_list_item_layout, pendingAc);
            Log.d("onReceive_widget", "----getViewAt----");
            return views;
        }

        @Override
        public RemoteViews getLoadingView() {
            /* 在更新界面的时候如果耗时就会显示 正在加载... 的默认字样，但是你可以更改这个界面
             * 如果返回null 显示默认界面
             * 否则 加载自定义的，返回RemoteViews
             */
            return null;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

    }

}
